package org.skydiveapplibrary.ui;

import org.skydiveapplibrary.R;

import com.actionbarsherlock.app.ActionBar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;

public class InterfaceManager {

	// singleton
	private static final InterfaceManager instance = new InterfaceManager();
	
	// frame definitions
	public static final int FRAME_LEFT = 10;
	public static final int FRAME_RIGHT = 20;
	public static final int FRAME_RIGHT_SUB = 30;
	public static final int FRAME_FULL_PAGE = 40;
	
	private InterfaceManager()
	{
	}
	
	public static InterfaceManager getInstance()
	{
		return instance;
	}
	
	public void replaceFragment(FragmentManager fm, int container,
			Fragment fragment, String tag)
	{
		replaceFragment(fm, container, fragment, tag, true, 0, 0);
	}
	
	public void replaceFragment(FragmentManager fm, int container,
			Fragment fragment, String tag, Boolean addToBackStack)
	{
		replaceFragment(fm, container, fragment, tag, addToBackStack, 0, 0);
	}
	
	public void replaceFragment(FragmentManager fm, int container,
			Fragment fragment, String tag, Boolean addToBackStack, int inAnimation, int outAnimation)
	{
		FragmentTransaction transaction = fm.beginTransaction();
		
		int viewId = 0;
		
		if (container == FRAME_LEFT)
			viewId = R.id.leftFrame;
		else if (container == FRAME_RIGHT)
			viewId = R.id.rightFrame;
//		else if (container == FRAME_RIGHT_SUB)
//			viewId = R.id.rightSubFrame;
		else if (container == FRAME_FULL_PAGE)
			viewId = R.id.fullPageFrame;
		else
			return;
		
		if (inAnimation != 0 || outAnimation != 0)
			transaction.setCustomAnimations(inAnimation, outAnimation, inAnimation, outAnimation);
		
		if (addToBackStack)
			transaction.addToBackStack(null);
		
		transaction.replace(viewId, fragment, tag);
	
		// Commit the transaction
		transaction.commit();
	}
	
	public void setSubtitleText(ActionBar ab, String title)
	{
		if (ab.getCustomView() != null)
		{
			TextView subtitleTextView = (TextView) ab.getCustomView().findViewById(R.id.actionbarSubtitle);
			subtitleTextView.setText(title);
		}
	}
}